package com.zjj.edu.web.controller.system;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.core.text.Convert;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.framework.util.UserPermissionUtil;
import com.zjj.edu.system.domain.SysTeachGoods;
import com.zjj.edu.system.domain.SysUser;
import com.zjj.edu.system.service.ISysTeachGoodsService;
import com.zjj.edu.system.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教学物品Controller
 *
 * @author zjj
 * @date 2020-06-30
 */
@Controller
@RequestMapping("/teach/goods")
public class SysTeachGoodsController extends BaseController
{
    private String prefix = "teach/goods";

    @Autowired
    private ISysTeachGoodsService sysTeachGoodsService;

    @Autowired
    private ISysTenantService tenantService;

    @RequiresPermissions("teach:goods:view")
    @GetMapping()
    public String goods(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/goods";
    }

    /**
     * 查询教学物品列表
     */
    @RequiresPermissions("teach:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTeachGoods sysTeachGoods)
    {
        startPage();
        sysTeachGoods.setTenantId(ShiroUtils.getTenantId());
        List<SysTeachGoods> list = sysTeachGoodsService.selectSysTeachGoodsList(sysTeachGoods);
        return getDataTable(list);
    }

    /**
     * 导出教学物品列表
     */
    @RequiresPermissions("teach:goods:export")
    @Log(title = "教学物品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTeachGoods sysTeachGoods)
    {
        sysTeachGoods.setTenantId(ShiroUtils.getTenantId());
        List<SysTeachGoods> list = sysTeachGoodsService.selectSysTeachGoodsList(sysTeachGoods);
        ExcelUtil<SysTeachGoods> util = new ExcelUtil<SysTeachGoods>(SysTeachGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 新增教学物品
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/add";
    }

    /**
     * 新增保存教学物品
     */
    @RequiresPermissions("teach:goods:add")
    @Log(title = "教学物品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTeachGoods sysTeachGoods)
    {
        sysTeachGoods.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysTeachGoodsService.checkNameUnique(sysTeachGoods))) {
            return error("新增教学物品'" + sysTeachGoods.getName() + "'失败，名称已存在");
        }
        sysTeachGoods.setNums(0L);
        sysTeachGoods.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysTeachGoodsService.insertSysTeachGoods(sysTeachGoods));
    }

    /**
     * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkTenantNameUnique(SysTeachGoods sysTeachGoods) {
        sysTeachGoods.setTenantId(ShiroUtils.getTenantId());
        return sysTeachGoodsService.checkNameUnique(sysTeachGoods);
    }

    /**
     * 修改教学物品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("sysTeachGoods", sysTeachGoodsService.selectSysTeachGoodsById(id));
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存教学物品
     */
    @RequiresPermissions("teach:goods:edit")
    @Log(title = "教学物品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTeachGoods sysTeachGoods)
    {
        sysTeachGoods.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysTeachGoodsService.checkNameUnique(sysTeachGoods))) {
            return error("修改教学物品'" + sysTeachGoods.getName() + "'失败，名称已存在");
        }
        sysTeachGoods.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysTeachGoodsService.updateSysTeachGoods(sysTeachGoods));
    }

    /**
     * 删除教学物品
     */
    @RequiresPermissions("teach:goods:remove")
    @Log(title = "教学物品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user = ShiroUtils.getSysUser();
        SysTeachGoods goods = new SysTeachGoods();
        goods.setIds(Convert.toStrArray(ids));
        goods.setUpdateBy(user.getUserName());
        if (!UserPermissionUtil.checkUserPermiss()) {
            goods.setTenantId(user.getTenantId());
        }
        return toAjax(sysTeachGoodsService.deleteSysTeachGoodsByIds(goods));
    }

    /**
     * 状态修改
     */
    @Log(title = "教学物品管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("teach:goods:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysTeachGoods sysTeachGoods)
    {
        sysTeachGoods.setTenantId(ShiroUtils.getTenantId());
        return toAjax(sysTeachGoodsService.updateSysTeachGoods(sysTeachGoods));
    }

    /**
     * 数量登记
     */
    @GetMapping("/numAdd/{id}")
    public String numAdd(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("teachGoods", sysTeachGoodsService.selectSysTeachGoodsById(id));
        return prefix + "/numAdd";
    }

}
